<template>
	<view class="page">
		<u-empty v-if="list.length == 0" mode="message" icon="http://cdn.uviewui.com/uview/empty/message.png">
		</u-empty>
		<uni-list>
			<uni-list-chat v-for="one in list" :key="one.id" @longpress="showModal(one.id)" title="机器人"
				:note="one.content" :avatar="one.avatar" badgePositon="left"
				:to="'/pages/check_message_content/check_message_content?content=' + one.content + '&shopId=' + one.shopId + '&result=' + one.result">
				<view class="chat-custom-right">
					<text v-if="Math.floor((Date.now() - one.sendTime) /60000) <= 60"
						class="chat-custom-text">{{Math.floor((Date.now() - one.sendTime) /60000) }}分钟前</text>
					<text
						v-if="Math.floor((Date.now() - one.sendTime) /60000) > 60 && Math.floor((Date.now() - one.sendTime) /60000) <= 60*24"
						class="chat-custom-text">{{Math.floor((Date.now() - one.sendTime) /(60000*60) ) }}小时前</text>
					<text v-if="Math.floor((Date.now() - one.sendTime) /60000) > 60 *24"
						class="chat-custom-text">{{Math.floor((Date.now() - one.sendTime) /(60000*60*24) ) }}天前</text>
				</view>
			</uni-list-chat>
		</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				messageId: '',
				show: false,
				userId: '',
				page: 1,
				length: 12,
				list: [],
				isLastPage: false
			}
		},
		onShow: function() {
			let that = this
			that.page = 1
			that.isLastPage = false
			console.log("askdiaujdiajdajdaidiajdiwajidjwaijdisdiwajdi")
			that.loadMessageList(that)
		},
		onLoad() {
			let that = this;
			that.userId = uni.getStorageSync("userId");
		},
		onReachBottom: function() {
			let that = this
			if (that.isLastPage) {
				return
			}
			that.page = that.page + 1
			that.loadMessageList(that)
		},
		methods: {
			cancelDelete() {
				this.show = false;
			},
			deleteItem() {
				let that = this;
				that.$ajax(that.$url.deleteMessage + "?messageId=" + that.messageId, "GET", null, function(resp) {
					uni.showToast({
						icon: 'none',
						title: '删除成功！'
					})
					that.show = false;
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/my_message/my_message'
						})
					}, 800)
				})
			},
			showModal(id) {
				let that = this;
				that.messageId = id;
				// 触发震动效果
				uni.vibrateShort();

				that.show = true;
			},
			goto(item) {
				let that = this;
				uni.navigateTo({
					url: '/pages/shop_content/shop_content?locationId=' + item.locationId
				})
			},
			loadMessageList: function(ref) {
				let that = this;
				let data = {
					start: ref.page,
					length: ref.length
				}
				ref.$ajax(ref.$url.searchMyCheckMessage, "POST", data, function(resp) {
					let result = resp.data.result
					if (result == null || result.length == 0) {
						ref.isLastPage = true
						ref.page = ref.page - 1
						uni.showToast({
							icon: "none",
							title: "已经到底了"
						})
					} else {
						if (ref.page == 1) {
							ref.list = []
						}
						ref.list = ref.list.concat(result)
						if (ref.page > 1) {
							uni.showToast({
								icon: "none",
								title: "又加载了" + result.length + "条消息"
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="less">
	@import url("my_check_message.less");
</style>